/*
utf8test v1.0 © 2006 Nikola Smolenski (smolensk@eunet.yu)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <stdio.h>

#define A ((unsigned char)a)

int main(int argc, char **argv)
{
	int a,rown=1,coln=1;
	char len=0,explen=0,res=0;

	while((a=getchar())!=EOF) {
		if(A==10) {
			rown++;
			coln=0;
		} else coln++;

		if(explen) {
			if((A&0xc0)==0x80) {
				len++;
/* TODO: check unnecessarily long sequence and issue a warning, per following:
U-00000000 - U-0000007F:        0xxxxxxx
U-00000080 - U-000007FF:        110xxxxx 10xxxxxx
U-00000800 - U-0000FFFF:        1110xxxx 10xxxxxx 10xxxxxx
U-00010000 - U-001FFFFF:        11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U-00200000 - U-03FFFFFF:        111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U-04000000 - U-7FFFFFFF:        1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
				if(len==explen) explen=0;
				continue;
			}
			explen=0;
			if(a<128) {
				res=1;
				printf("%d:%d: error: Sequence terminated with 7-bit character %X\n",rown,coln,A);
				continue;
			} else {
				res=1;
				printf("%d:%d: error: Sequence terminated with new sequence\n",rown,coln);
			}
		}

		if(A<128) continue;

		if((A&0xc0)==0xc0) {
// TODO: draw length from a table
			for(a=A<<1;A&0x80;a=A<<1,explen++);
			if(explen>5) {
				res=1;
				printf("%d:%d: error: Sequence too long\n",rown,coln);
				explen=0;
				continue;
			}
			len=0;
		} else {
			res=1;
			printf("%d:%d: error: 8-bit character %X outside of a sequence\n",rown,coln,A);
		}
	}

	return res;
}
